<template>
  <div class="system-role-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="600px" @close="closeDialog" :close-on-click-modal="false">
      <el-form ref="dialogFormRef" :model="state.ruleForm" size="default" label-width="100px"
        v-loading="state.dataLoading" label-position="right">

        <el-form-item label="任务名称" prop="name">
          <el-input v-model="state.ruleForm.taskPackageInfo.name" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="单位名称" prop="unit">
          <el-input v-model="state.ruleForm.taskPackageInfo.unit" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="指标名称" prop="name">
          <el-input v-model="state.ruleForm.name" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="难度等级" prop="level">
          <el-input v-model="state.ruleForm.level" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="state.ruleForm.price" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="参考信源" prop="refer">
          <el-input v-model="state.ruleForm.refer" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="名称" :prop="'dynamicItem.' + index+ '.name'" v-for="(item, index) in state.dynamicItem" :key="index">
          <el-input v-model="item.extend_name" placeholder="请输入标题" clearable style="width: 40%;" :disabled="true"></el-input>
          <div style="width: 10%;">内容:</div>
          <el-input v-model="item.extend_value" placeholder="请输入标题对应的值" clearable style="width: 50%;" :disabled="true"></el-input>
        </el-form-item>
        <el-divider><span style="color: #F56C6C;">用户需填写区域</span></el-divider>
        <el-form-item label="名称" :prop="'userFilled.' + index+ '.name'" v-for="(item, index) in state.userFilled" :key="index">
          <el-input v-model="item.extend_name" placeholder="请输入标题" clearable style="width: 40%;" :disabled="true"></el-input>
          <div style="width: 20%;">是否必填:</div>
          <el-radio v-model='item.required' label="1" :disabled="true">是</el-radio>
          <el-radio v-model='item.required' label="0" :disabled="true">否</el-radio>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog()" size="default">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="systemRoleDialog">
import { systemApi } from '/@/api/system'
import { ElMessage } from 'element-plus';
import { toolsApi } from '/@/api/tools'
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dialogFormRef = ref();
const state = reactive({
  userId: null,
  ruleForm: {
   
  },
  dialog: {
    isShowDialog: false,
    title: '新增用户',
    submitTxt: '',
  },
  dataLoading: false,
  btnLoading: false,
  dynamicItem:[],
  userFilled:[],
});

// 打开弹窗
const openDialog = (row) => {

  state.dialog.isShowDialog = true;
  if (row) {
    state.dialog.title = '查看详情';
    state.ruleForm = row
    state.dynamicItem = row.normListExtend
    state.userFilled = row.user_filled
  }
};
// 关闭弹窗
const closeDialog = () => {
  dialogFormRef.value.resetFields();
  state.dialog.isShowDialog = false;
};


// 提交
const onSubmit = () => {
  dialogFormRef.value.validate((valid, fields) => {
    if (valid) {
      state.btnLoading = true
      const apiData = JSON.parse(JSON.stringify(state.ruleForm))
      if (state.userId) { // 编辑
        apiData.id = state.userId
        systemApi().editUser(apiData).then(res => {
          state.btnLoading = false
          ElMessage.success('操作成功');
          closeDialog();
          emit('refresh');
        }).catch(() => {
          state.btnLoading = false
          closeDialog();
          emit('refresh');
        })
      } else {
        systemApi().addUser(apiData).then(res => {
          state.btnLoading = false
          ElMessage.success('操作成功');
          closeDialog();
          emit('refresh');
        }).catch(() => {
          state.btnLoading = false
          closeDialog();
          emit('refresh');
        })
      }

    }

  })


};



// 暴露变量
defineExpose({
  openDialog,
});


onMounted(() => {

});
</script>

<style scoped lang="scss"></style>
